# 以下三行是汉化过程中测试用代码，学习者在自己的环境中应删除或注释掉
导入 sys
导入 os
sys.path.追加(os.路径.绝对路径(r'G:\grasspy_zwmodules\selenium\zwselenium\src'))


from 自动访问 import 驱动
从 自动访问.驱动 导入 〇动作链
从 自动访问.驱动.公用.动作.动作构建器 导入 〇动作构建器
从 自动访问.驱动.公用.依据 导入 〇依据
from 自动访问.驱动.edge.服务 import 〇服务
# from selenium.webdriver.common.actions.wheel_input import ScrollOrigin
从 自动访问.驱动.公用.动作.滚轮输入 导入 〇滚动原点
从 时间 导入 睡眠

服务 = 〇服务(可执行文件路径="G:\grasspy_zwmodules\selenium\edgedriver_win64\msedgedriver.exe")
老司机 = 驱动.Edge(服务=服务)

def _in_viewport(老司机, element):
    script = (
        "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"
        "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"
        "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n"
        "window.pageYOffset&&t+o>window.pageXOffset"
    )
    return 老司机.执行脚本(script, element)

def test_can_scroll_to_element(老司机):
    老司机.获取("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

    iframe = 老司机.查找元素(〇依据.匚标签名, "iframe")
    〇动作链(老司机)\
        .滚动至元素(iframe)\
        .完成()

    assert _in_viewport(老司机, iframe)

# test_can_scroll_to_element(老司机)

def test_can_scroll_from_viewport_by_amount(老司机):
    老司机.获取("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

    footer = 老司机.查找元素(〇依据.匚标签名, "footer")
    delta_y = footer.矩形['y']
    〇动作链(老司机)\
        .滚动一定量(0, delta_y)\
        .完成()

    睡眠(2)
    assert _in_viewport(老司机, footer)

# test_can_scroll_from_viewport_by_amount(老司机)

def test_can_scroll_from_element_by_amount(老司机):
    老司机.获取("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

    iframe = 老司机.查找元素(〇依据.匚标签名, "iframe")
    scroll_origin = 〇滚动原点.从元素(iframe)
    〇动作链(老司机)\
        .从指定原点滚动(scroll_origin, 0, 200)\
        .完成()

    睡眠(2)
    老司机.切换到.框架(iframe)
    checkbox = 老司机.查找元素(〇依据.匚名称, "scroll_checkbox")
    assert _in_viewport(老司机, checkbox)

# test_can_scroll_from_element_by_amount(老司机)

def test_can_scroll_from_element_with_offset_by_amount(老司机):
    老司机.获取("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")

    footer = 老司机.查找元素(〇依据.匚标签名, "footer")
    scroll_origin = 〇滚动原点.从元素(footer, 0, -50)
    〇动作链(老司机)\
        .从指定原点滚动(scroll_origin, 0, 200)\
        .完成()

    睡眠(2)
    iframe = 老司机.查找元素(〇依据.匚标签名, "iframe")
    老司机.切换到.框架(iframe)
    checkbox = 老司机.查找元素(〇依据.匚名称, "scroll_checkbox")
    assert _in_viewport(老司机, checkbox)

# test_can_scroll_from_element_with_offset_by_amount(老司机)

def test_can_scroll_from_viewport_with_offset_by_amount(老司机):
    老司机.获取("https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html")

    scroll_origin = 〇滚动原点.from_viewport(10, 10)

    〇动作链(老司机)\
        .从指定原点滚动(scroll_origin, 0, 200)\
        .完成()

    睡眠(3)
    iframe = 老司机.查找元素(〇依据.匚标签名, "iframe")
    老司机.切换到.框架(iframe)
    checkbox = 老司机.查找元素(〇依据.匚名称, "scroll_checkbox")
    assert _in_viewport(老司机, checkbox)

# test_can_scroll_from_viewport_with_offset_by_amount(老司机)
